package com.tools.acceleratecore.xlib.util;

import android.content.Context;
import android.text.TextUtils;

import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.mode.intf.IXJsonSerialization;
import com.tools.acceleratecore.xlib.xlib.intf.IXFactory;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class UtilJson {
    private static final String VALUE_STRING_JSON_KEY = "ludashinb";
    private static IXFactory sIXFactory = XLibFactory.getInstance();

    public UtilJson() {
    }

    public static void setFactory(IXFactory iXFactory) {
        sIXFactory = iXFactory;
    }

    public static boolean saveJsonToFile(Context context, String strFileName, JSONObject jsonObject) {
        if (context != null && !TextUtils.isEmpty(strFileName) && jsonObject != null) {
            try {
                FileOutputStream fOut = context.openFileOutput(strFileName, 0);
                byte[] bytes = jsonObject.toString().getBytes();
                fOut.write(bytes);
                fOut.flush();
                fOut.close();
                return true;
            } catch (Exception var5) {
                var5.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean saveJsonToFileWithEncrypt(Context context, String strFileName, JSONObject jsonObject) {
        if (context != null && !TextUtils.isEmpty(strFileName) && jsonObject != null) {
            try {
                FileOutputStream fOut = context.openFileOutput(strFileName, 0);
                byte[] bytes = jsonObject.toString().getBytes();
                bytes = UtilEncrypt.encryptBlowFish(bytes, "ludashinb");
                fOut.write(bytes);
                fOut.flush();
                fOut.close();
                return true;
            } catch (Exception var5) {
                var5.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }

    public static JSONObject loadJsonFromFile(Context context, String strFileName) {
        if (context != null && !TextUtils.isEmpty(strFileName)) {
            try {
                FileInputStream fIn = context.openFileInput(strFileName);
                if (fIn == null) {
                    return null;
                } else {
                    byte[] buffer = new byte[UtilEnv.VALUE_INT_BUFFER_SIZE];
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

                    int readCount;
                    while ((readCount = fIn.read(buffer)) != -1) {
                        outStream.write(buffer, 0, readCount);
                    }

                    fIn.close();
                    outStream.close();
                    return new JSONObject(outStream.toString());
                }
            } catch (Exception var6) {
                var6.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static JSONObject loadJsonFromFileWithDecrypt(Context context, String strFileName) {
        if (context != null && !TextUtils.isEmpty(strFileName)) {
            try {
                FileInputStream fIn = context.openFileInput(strFileName);
                if (fIn == null) {
                    return null;
                } else {
                    byte[] buffer = new byte[UtilEnv.VALUE_INT_BUFFER_SIZE];
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

                    int readCount;
                    while ((readCount = fIn.read(buffer)) != -1) {
                        outStream.write(buffer, 0, readCount);
                    }

                    fIn.close();
                    outStream.close();
                    String strJson = new String(UtilEncrypt.decryptBlowFish(outStream.toByteArray(), "ludashinb"));
                    return new JSONObject(strJson);
                }
            } catch (Exception var7) {
                var7.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static <T> void JsonSerialization(JSONObject jsonObject, String strName, T tValue) {
        if (jsonObject != null && !TextUtils.isEmpty(strName)) {
            try {
                if (IXJsonSerialization.class.isAssignableFrom(tValue.getClass())) {
                    IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) tValue;
                    jsonObject.put(strName, iXJsonSerialization.Serialization());
                } else {
                    jsonObject.put(strName, tValue);
                }
            } catch (Exception var4) {
                var4.printStackTrace();
            }

        }
    }

    public static <T> void JsonSerialization(JSONObject jsonObject, String strName, List<T> listValue) {
        if (jsonObject != null && !TextUtils.isEmpty(strName) && listValue != null) {
            try {
                JSONArray jArray = new JSONArray();
                Iterator var5 = listValue.iterator();

                while (var5.hasNext()) {
                    T t = (T) var5.next();
                    if (IXJsonSerialization.class.isAssignableFrom(t.getClass())) {
                        IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) t;
                        jArray.put(iXJsonSerialization.Serialization());
                    } else {
                        jArray.put(t);
                    }
                }

                jsonObject.put(strName, jArray);
            } catch (Exception var7) {
                var7.printStackTrace();
            }

        }
    }

    public static <T> void JsonSerialization(JSONObject jsonObject, String strName, Set<T> setValue) {
        if (jsonObject != null && !TextUtils.isEmpty(strName) && setValue != null) {
            try {
                JSONArray jArray = new JSONArray();
                Iterator var5 = setValue.iterator();

                while (var5.hasNext()) {
                    T t = (T) var5.next();
                    if (IXJsonSerialization.class.isAssignableFrom(t.getClass())) {
                        IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) t;
                        jArray.put(iXJsonSerialization.Serialization());
                    } else {
                        jArray.put(t);
                    }
                }

                jsonObject.put(strName, jArray);
            } catch (Exception var7) {
                var7.printStackTrace();
            }

        }
    }

    public static <T1, T2> void JsonSerialization(JSONObject jsonObject, String strName, Map<T1, T2> mapValue) {
        if (jsonObject != null && !TextUtils.isEmpty(strName) && mapValue != null) {
            try {
                JSONObject jObject = new JSONObject();
                Set<Entry<T1, T2>> set = mapValue.entrySet();
                Iterator iter = set.iterator();

                while (iter.hasNext()) {
                    Entry<T1, T2> entry = (Entry) iter.next();
                    JsonSerialization(jObject, String.valueOf(entry.getKey()), entry.getValue());
                }

                jsonObject.put(strName, jObject);
            } catch (Exception var7) {
                var7.printStackTrace();
            }

        }
    }

    public static <T> T JsonUnserialization(JSONObject jsonObject, String strName, T tValue) {
        Object value = null;
        if (jsonObject != null && !TextUtils.isEmpty(strName) && tValue != null) {
            try {
                if (tValue.getClass() == Integer.class) {
                    value = jsonObject.getInt(strName);
                } else if (tValue.getClass() == Long.class) {
                    value = jsonObject.getLong(strName);
                } else if (tValue.getClass() == Boolean.class) {
                    value = jsonObject.getBoolean(strName);
                } else if (tValue.getClass() == Double.class) {
                    value = jsonObject.getDouble(strName);
                } else if (tValue.getClass() == String.class) {
                    value = jsonObject.getString(strName);
                } else if (IXJsonSerialization.class.isAssignableFrom(tValue.getClass())) {
                    IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) tValue;
                    iXJsonSerialization.Unserialization(jsonObject.getJSONObject(strName));
                }
            } catch (Exception var4) {
                var4.printStackTrace();
            }

            return (T) value;
        } else {
            return null;
        }
    }

    public static <T> void JsonUnserialization(JSONObject jsonObject, String strName, List<T> listValue, Class<?> classT, Class<?> classInterface, Class<?> classImplement) {
        if (jsonObject != null && !TextUtils.isEmpty(strName) && listValue != null && classT != null) {
            try {
                listValue.clear();
                JSONArray jArray = jsonObject.getJSONArray(strName);

                for (int nIndex = 0; nIndex < jArray.length(); ++nIndex) {
                    Object tValue = null;
                    if (classT == Integer.class) {
                        tValue = jArray.getInt(nIndex);
                    } else if (classT == Long.class) {
                        tValue = jArray.getLong(nIndex);
                    } else if (classT == Boolean.class) {
                        tValue = jArray.getBoolean(nIndex);
                    } else if (classT == Double.class) {
                        tValue = jArray.getDouble(nIndex);
                    } else if (classT == String.class) {
                        tValue = jArray.getString(nIndex);
                    } else if (IXJsonSerialization.class.isAssignableFrom(classT)) {
                        tValue = sIXFactory.createInstance(classInterface, classImplement);
                        IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) tValue;
                        iXJsonSerialization.Unserialization(jArray.getJSONObject(nIndex));
                    }

                    if (tValue != null) {
                        listValue.add((T) tValue);
                    }
                }
            } catch (Exception var10) {
                var10.printStackTrace();
            }

        }
    }

    public static <T> void JsonUnserialization(JSONObject jsonObject, String strName, Set<T> setValue, Class<?> classT, Class<?> classInterface, Class<?> classImplement) {
        if (jsonObject != null && !TextUtils.isEmpty(strName) && setValue != null && classT != null) {
            try {
                setValue.clear();
                JSONArray jArray = jsonObject.getJSONArray(strName);

                for (int nIndex = 0; nIndex < jArray.length(); ++nIndex) {
                    Object tValue = null;
                    if (classT == Integer.class) {
                        tValue = jArray.getInt(nIndex);
                    } else if (classT == Long.class) {
                        tValue = jArray.getLong(nIndex);
                    } else if (classT == Boolean.class) {
                        tValue = jArray.getBoolean(nIndex);
                    } else if (classT == Double.class) {
                        tValue = jArray.getDouble(nIndex);
                    } else if (classT == String.class) {
                        tValue = jArray.getString(nIndex);
                    } else if (IXJsonSerialization.class.isAssignableFrom(classT)) {
                        tValue = sIXFactory.createInstance(classInterface, classImplement);
                        IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) tValue;
                        iXJsonSerialization.Unserialization(jArray.getJSONObject(nIndex));
                    }

                    if (tValue != null) {
                        setValue.add((T) tValue);
                    }
                }
            } catch (Exception var10) {
                var10.printStackTrace();
            }

        }
    }

    public static <T1, T2> void JsonUnserialization(JSONObject jsonObject, String strName, Map<T1, T2> mapValue, Class<?> classT1, Class<?> classT2, Class<?> classInterface2, Class<?> classImplement2) {
        if (jsonObject != null && !TextUtils.isEmpty(strName) && mapValue != null && classT1 != null && classT2 != null) {
            try {
                mapValue.clear();
                JSONObject jObject = jsonObject.getJSONObject(strName);

                Object key;
                Object value;
                for (Iterator iter = jObject.keys(); iter.hasNext(); mapValue.put((T1) key, (T2) value)) {
                    String strKey = (String) iter.next();
                    key = null;
                    if (classT1 == Integer.class) {
                        key = Integer.valueOf(strKey);
                    } else if (classT1 == Long.class) {
                        key = Long.valueOf(strKey);
                    } else if (classT1 == Boolean.class) {
                        key = Boolean.valueOf(strKey);
                    } else if (classT1 == Double.class) {
                        key = Double.valueOf(strKey);
                    } else if (classT1 == String.class) {
                        key = strKey;
                    }

                    value = null;
                    if (classT2 == Integer.class) {
                        value = jObject.getInt(strKey);
                    } else if (classT2 == Long.class) {
                        value = jObject.getLong(strKey);
                    } else if (classT2 == Boolean.class) {
                        value = jObject.getBoolean(strKey);
                    } else if (classT2 == Double.class) {
                        value = jObject.getDouble(strKey);
                    } else if (classT2 == String.class) {
                        value = jObject.getString(strKey);
                    } else if (IXJsonSerialization.class.isAssignableFrom(classT2)) {
                        value = sIXFactory.createInstance(classInterface2, classImplement2);
                        IXJsonSerialization iXJsonSerialization = (IXJsonSerialization) value;
                        iXJsonSerialization.Unserialization(jObject.getJSONObject(strKey));
                    }
                }
            } catch (Exception var13) {
                var13.printStackTrace();
            }

        }
    }
}
